package org.FaPe3D.core.settings
{
	
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import flash.events.*;
	import flash.net.*;
	import flash.utils.Dictionary;
	import flash.xml.*;
	
	import org.FaPe3D.core.events.EventManager;
	import org.FaPe3D.core.parsers.*;
	
	public class Settings extends EventManager
	{
		private var _id:String;
		
		private var _bulkLoader:BulkLoader;
		private var _LOADER_KEY = 'setting_';
		private var _setting:Dictionary;
				
		public function Settings(id:String, settingPath:String)
		{
			_id = id;
			_setting = new Dictionary(true);
			_LOADER_KEY = _LOADER_KEY + _id;
			
			// cretae bulkLoader instance
			_bulkLoader = new BulkLoader('FAPE3D_' + 'settings' + _id );
			
			// add file to bulk loader
			_bulkLoader.add(settingPath, {type:"xml", id: _LOADER_KEY});
			
			// start loading
			_bulkLoader.start();
			// setup events
			_bulkLoader.addEventListener(BulkProgressEvent.COMPLETE, onCompelete);
			_bulkLoader.addEventListener(BulkLoader.PROGRESS, loadingProgress);
			_bulkLoader.addEventListener(BulkLoader.ERROR, loadingError)
		}
		
		public function get setting():Dictionary
		{
			return _setting;
		}
		
		public function set setting(dic:Dictionary):void
		{
			_setting = dic;
		}
		
		private function onCompelete(e:*):void
		{
			// loading compeleted.
			
			// get content and clear memory
			var xml:XML = _bulkLoader.getXML( _LOADER_KEY, true );
			setting = Setting.parse( xml );
			// remove bulkloader instance
			_bulkLoader.removeAll();
			//
			dispatchEvent(new Event(Event.COMPLETE));		
		}
		
		private function loadingProgress(e:*):void
		{
//			trace('loading progress', e.bytesLoaded , '/', e.bytesTotal);
		}
		
		private function loadingError(e:*):void
		{
			trace('error in loading xml file. (setting)' , e);
		}
	}
}